//
//  MunicipalityView.m
//  CarHome
//
//  Created by 张世琦 on 14-9-24.
//  Copyright (c) 2014年 WJN_work@163.com. All rights reserved.
//

#import "MunicipalityView.h"
#import "ProvincesModel.h"
#import "CitysModel.h"
#import "CityIdModel.h"

NSString *const municipalityCell = @"cellId";

@implementation MunicipalityView

- (void)dealloc
{
    [_array release];
    _array = nil;
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _array = [[NSMutableArray alloc] init];
        [self createTableView];
    }
    return self;
}

#pragma mark -
#pragma mark 创建TableView

- (void)createTableView
{
    UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 240, self.frame.size.height - 20)];
    aView .backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    UIView *bView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 240, 44)];
    bView.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, 80, 44)];
    label.text = @"选择城市";
    [bView addSubview:label];
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setFrame:CGRectMake(190, 0, 50, 44)];
    [rightButton setTitle:@"关闭" forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [bView addSubview:rightButton];
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setFrame:CGRectMake(0, 0, 50, 44)];
    [leftButton setTitle:@"返回" forState:UIControlStateNormal];
    [leftButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
    [bView addSubview:leftButton];
    [aView addSubview:bView];
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, 240, self.frame.size.height - 226) style:UITableViewStylePlain];
    [_table registerClass:[UITableViewCell class] forCellReuseIdentifier:municipalityCell];
    _table.dataSource = self;
    _table.delegate = self;
    [aView addSubview:_table];
    [self addSubview:aView];
    
}

#pragma mark -
#pragma mark 点击事件

- (void)buttonAction:(id)sender
{
    [self.superview removeFromSuperview];
}

- (void)button:(id)sender
{
    [self removeFromSuperview];
}

#pragma mark -
#pragma mark UITableViewDataSource协议

//每个section行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.array count];
}

//重用机制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:municipalityCell forIndexPath:indexPath];
    [cell.textLabel setText:[[self.array objectAtIndex:indexPath.row] objectForKey:@"name"]];
    return cell;
}

#pragma mark -
#pragma mark UITableViewUITableViewDelegate协议

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.cityId = [[[self.array objectAtIndex:indexPath.row] objectForKey:@"id"] intValue];
    CityIdModel *model = [[CityIdModel alloc] init];
    model.provinceId = self.provinceId;
    model.cityId = self.cityId;
    model.cityname = [[self.array objectAtIndex:indexPath.row] objectForKey:@"name"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"chengshi" object:model];
    [self.superview removeFromSuperview];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
